layui.define(['form','table','element'], function(exports){
  var $ = layui.$,form = layui.form,table = layui.table,element = layui.element;
  var tradeNo = $('input[name="tradeNo"]').val();

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/cdcz/order/operate/record/list.data'
    ,where:{tradeNo:tradeNo}
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
      {field: 'createDate',width:170,title: '操作时间'}
      ,{field: 'content',title: '操作内容'}
      ,{field: 'programTypeName', width:120,title: '处理程序'}
      ,{field: 'operatorType', width:150,title: '操作人类型',templet:function (record){
            return "["+record.operatorTypeName+"]" + record.operator;
      }}
      ,{field: 'state', title: '状态',templet:function (record){
          if(record.state == 1){
              return "成功";
          }else {
              return "失败（"+record.errorMsg+"）";
          }
      }}
    ]]
    ,page: false
    ,height: 'full'
    ,text: '对不起，加载出现异常！'
  });

    table.render({
        elem: '#LAY-table-charge'
        ,url: layui.setter.webRoot + '/cdcz/order/charge/record/list.data'
        ,where:{tradeNo:tradeNo}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'createDate',width:170,title: '操作时间'}
            ,{field: 'content',title: '操作内容'}
            ,{field: 'programTypeName', width:120,title: '处理程序'}
            ,{field: 'operatorType', width:150,title: '操作人类型',templet:function (record){
                    return "["+record.operatorTypeName+"]" + record.operator;
                }}
            ,{field: 'state', title: '状态',templet:function (record){
                    if(record.state == 1){
                        return "成功";
                    }else {
                        return "失败（"+record.errorMsg+"）";
                    }
                }}
        ]]
        ,page: false
        ,height: 'full'
        ,text: '对不起，加载出现异常！'
    });

  //事件
  var active = {
      finish:function(){
          layer.open({
              type: 2
              ,title: '完成充电插座订单'
              ,content: layui.setter.webRoot + '/cdcz/order/operate/finish.html?tradeNo='+tradeNo
              ,maxmin: true
              ,area: ['400px', '400px']
              ,btn: ['确定', '取消']
              ,yes: function (index, layero) {
                  var iframeWindow = window['layui-layer-iframe' + index]
                      , submitID = 'LAY-form-submit'
                      , submit = layero.find('iframe').contents().find('#' + submitID);
                  iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/cdcz/order/operate/finish.do',
                          data: field,
                          dataType: "json",
                          success: function (res) {
                              if (res.code == 200) {
                                  layer.close(index); //关闭弹层
                                  layer.msg('完成成功', {icon: 1},function (){
                                      location.reload();
                                  });
                              } else {
                                  layer.msg(res.msg, {icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,refund:function(){
          layer.open({
              type: 2
              ,title: '充电插座订单退款'
              ,content: layui.setter.webRoot + '/cdcz/order/operate/refund.html?tradeNo='+tradeNo
              ,maxmin: true
              ,area: ['400px', '400px']
              ,btn: ['确定', '取消']
              ,yes: function (index, layero) {
                  var iframeWindow = window['layui-layer-iframe' + index]
                      , submitID = 'LAY-form-submit'
                      , submit = layero.find('iframe').contents().find('#' + submitID);
                  iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/cdcz/order/operate/refund.do',
                          data: field,
                          dataType: "json",
                          success: function (res) {
                              if (res.code == 200) {
                                  layer.close(index); //关闭弹层
                                  layer.msg('退款成功', {icon: 1},function (){
                                      location.reload();
                                  });
                              } else {
                                  layer.msg(res.msg, {icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
     }
     ,cancel:function(){
          layer.open({
              type: 2
              ,title: '撤销充电插座订单'
              ,content: layui.setter.webRoot + '/cdcz/order/operate/cancel.html?tradeNo='+tradeNo
              ,maxmin: true
              ,area: ['400px', '400px']
              ,btn: ['确定', '取消']
              ,yes: function (index, layero) {
                  var iframeWindow = window['layui-layer-iframe' + index]
                      , submitID = 'LAY-form-submit'
                      , submit = layero.find('iframe').contents().find('#' + submitID);
                  iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/cdcz/order/operate/cancel.do',
                          data: field,
                          dataType: "json",
                          success: function (res) {
                              if (res.code == 200) {
                                  layer.close(index); //关闭弹层
                                  layer.msg('撤销成功', {icon: 1},function (){
                                      location.reload();
                                  });
                              } else {
                                  layer.msg(res.msg, {icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
  };

    element.on('tab(tab-info)', function (data) {
        if (data.index == 0) {
            table.reload('LAY-table')
        }else if(data.index == 1) {
            table.reload('LAY-table-charge')
        }
    });
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('charge_socket_order_operate', {})
});